/**
 * @Date 2025/1/8 16:41
 * @Created by 衡盛永
 */

package org.example.deploy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.deploy.dto.ResultDto;
import org.example.deploy.pojo.SysRole;
import org.example.deploy.service.SysRoleService;
import org.example.deploy.vo.role.AssignRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("list")
    public ResultDto<IPage<SysRole>> list(@RequestParam Map<String, String> params) {
        int current = Integer.parseInt(params.get("page"));
        int size = Integer.parseInt(params.get("size"));
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        Page<SysRole> page = new Page<>(current, size);
        sysRoleService.page(page, wrapper);
        return ResultDto.success("获取角色列表成功", page);
    }

    @RequestMapping("add")
    public ResultDto<Boolean> addRole(SysRole role) {
        boolean b = sysRoleService.save(role);
        if (b) {
            return ResultDto.success("新增角色成功", true);
        } else {
            return ResultDto.error(201, "新增角色失败");
        }
    }

    @PostMapping("assign_role")
    public ResultDto<Boolean> assignRole(@RequestBody AssignRoleVo assignRoleVo) {
        System.out.println("assignRoleVo:" + assignRoleVo);
        boolean b = sysRoleService.assignRole(assignRoleVo.getRoleId(), assignRoleVo.getUserId());
        return ResultDto.success("分配角色成功", b);
    }
}
